using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net.Mail;


namespace Logic.Utils
{
	public class Email
	{

        public static bool SendEmail(string subject, string body, string email, string senderAddress, bool useSsl)
        {
            try
            {
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
                
                //create and send the email
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();                
                message.To.Add(email);
                message.Subject = subject;
                message.SubjectEncoding = Encoding.UTF8;
                message.Body = body;
                message.IsBodyHtml = true;
                message.BodyEncoding = Encoding.UTF8;

                //if a sender address is specified, use it.
                //if not use the SMTP default sender property(by not specifing the From property of the MailMessage)
                if (!string.IsNullOrEmpty(senderAddress))
                    message.From = new System.Net.Mail.MailAddress(senderAddress);
                

                smtp.EnableSsl = useSsl;
                smtp.Send(message);
            }
            catch (Exception ex)
            {                     
                throw ex;
            }

            return true;
            
        }
        public static bool SendEmail(string subject, string body, string email, string senderAddress, bool useSsl, string username,string password)
        {
            try
            {
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
                smtp.Credentials = new System.Net.NetworkCredential(username, password);

                //create and send the email
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                message.To.Add(email);
                message.Subject = subject;
                message.SubjectEncoding = Encoding.UTF8;
                message.Body = body;
                message.IsBodyHtml = true;
                message.BodyEncoding = Encoding.UTF8;

                //if a sender address is specified, use it.
                //if not use the SMTP default sender property(by not specifing the From property of the MailMessage)
                if (!string.IsNullOrEmpty(senderAddress))
                    message.From = new System.Net.Mail.MailAddress(senderAddress);


                smtp.EnableSsl = useSsl;
                smtp.Send(message);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return true;

        }
        //public static bool SendEmailViaGoogleApps(string username, string password, string subject, string body, string email, string senderAddress)
        //{
        //    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
        //    message.From = new System.Net.Mail.MailAddress(senderAddress);
        //    message.To.Add(email);
        //    message.Subject = subject;
        //    message.SubjectEncoding = Encoding.UTF8;
        //    message.Body = body;
        //    message.IsBodyHtml = true;
        //    message.BodyEncoding = Encoding.UTF8;

        //    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
        //    client.EnableSsl = true;
        //    client.Send(message);

        //}

		/// <summary>
		/// sends an email to the email address using the template by it's name frrom 
		/// the /EmailTemplates directory. all files there are HTMLs
		/// the argumets in the template are replaced with their values according to their keys
		/// </summary>
		/// <param name="templateName"></param>
		/// <param name="email"></param>
		/// <param name="subject"></param>
		/// <param name="keys"></param>
		/// <param name="values"></param>
		/// <returns></returns>
		public static bool SendByTemplate(string templateName, string email, string subject,string senderAddress, string[] keys, string[] values)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();

			string serverUrl = HttpContext.Current.Request.Url.Host.ToString();
			if (HttpContext.Current.Request.Url.Port.ToString() != "80")
				serverUrl += ":" + HttpContext.Current.Request.Url.Port.ToString();

			//load template
			sb.Append(System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("/EmailTemplates/" + templateName + ".html")));

			//take care of the base URL
			sb.Replace("[@ServerUrl]", serverUrl);

			//loop thru arguments
			for (int i = 0; i < keys.Length; i++)
			{
				sb.Replace("[@" + keys[i] + "]", values[i]);
			}

			try
			{

				//create and send the email
				System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
				message.From = new System.Net.Mail.MailAddress(senderAddress);
				message.To.Add(email);
				message.Subject = subject;
				message.SubjectEncoding = Encoding.UTF8;
				message.Body = sb.ToString();
				message.IsBodyHtml = true;
				message.BodyEncoding = Encoding.UTF8;

				System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
				smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

				smtp.Send(message);
			}
			catch
			{
                return false;
				// do nothing when fail.
			}

            return true;
		}
	}
}
